﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Blokus.Scenes;
using Microsoft.Xna.Framework.GamerServices;
using PersistenceLayer;

namespace Blokus
{
    public class Blokus : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        private List<Scene> scenes = new List<Scene>();
        private GameState gameState = GameState.MENU;

        public enum GameState
        {
            MENU = 0,
            PLAYING = 1,
            HIGHSCORES = 2,
            CONTROLS = 3,
            CREDITS = 4,
            DESIGNER_MENU = 5,
            DESIGNER_PIECE = 6,
            DESIGNER_BOARD = 7,
            NEWGAME = 8,
            DESIGNER_CREATION = 9,
            DESIGNER_EDIT = 10,
            EXIT = 11,
        }

        public Blokus()
        {
            this.IsMouseVisible = true;
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 800;
            //graphics.IsFullScreen = true;
            this.IsFixedTimeStep = false;	// Force an update call as often as possible
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Components.Add(new GamerServicesComponent(this));
            //Create all the scenes
            scenes.Add(new SceneMenu(this));
            scenes.Add(new ScenePlaying(this)); //BJ's game goes here
            scenes.Add(new SceneHighscores(true));
            scenes.Add(new SceneControls());
            scenes.Add(new SceneCredits());

            scenes.Add(new SceneDesignerMenu());
            scenes.Add(new SceneDesigner(this, 5, 5)); //Designer Piece
            scenes.Add(new SceneDesigner(this, 10, 10)); //Designer Board
            scenes.Add(new SceneNewGame()); //Designer Board
            scenes.Add(new SceneGameCreator(this)); //Piece Creator
            scenes.Add(new SceneEditGame()); //Piece Creator

            PDesigner.initialize();
            this.IsMouseVisible = false;
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            foreach (Scene scene in scenes)
            {
                scene.LoadContent(this.Content, graphics);
            }
            Audio.Audio.initializeAudio(this.Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            GameState prevGameState = gameState;
            gameState = scenes[(int)gameState].Update(gameTime);
            if (gameState != prevGameState && gameState != GameState.EXIT)
                scenes[(int)gameState].ResetPrevKeys();
            //start new game if press new game
            if (gameState == GameState.PLAYING && prevGameState == GameState.NEWGAME)
            {
                ScenePlaying r = scenes[(int)gameState] as ScenePlaying;
                r.newGame();
            }
            if (gameState == GameState.EXIT)
            {
                this.Exit();
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            scenes[(int)gameState].Draw(gameTime);

            base.Draw(gameTime);
        }
    }
}
